package org.sjs.dreamfly.common.base

import org.sjs.dreamfly.common.util.LogUtil
import java.lang.ref.WeakReference

interface Listener {
    fun notify(vararg params: Any?)
}

interface ListenerOwner<T: Listener> {
    val listeners: MutableList<WeakReference<T>>
    fun notifyListeners(vararg params: Any?) {
        LogUtil.d("${hashCode()} - listeners:${listeners.hashCode()} size:${listeners.size}")
        listeners.forEach {
            it.get()?.notify(params)
        }
    }
    fun addListener(listener: T) {
        LogUtil.d("[before] ${hashCode()} - listeners:${listeners.hashCode()} size:${listeners.size}")
        listeners.add(WeakReference(listener))
        LogUtil.d("[after] ${hashCode()} - listeners:${listeners.hashCode()} size:${listeners.size}")
    }
}